import {Injectable} from '@angular/core';
import {Facility} from '../types/resp/Facility';
import {FacilityForm} from '../types/req/FacilityForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {FacilityQueryForm} from '../types/req/FacilityQueryForm';

@Injectable({
  providedIn: 'root'
})
export class FacilityService {
  facilitys: Facility[] = [
    {id: 1, name: '充电应急灯', buildingId: 1, buildingName: '北区1栋', location: '二楼', lastMaintenanceDate: '2024-01-01', status: 10, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, name: '高空摄像头', buildingId: 2, buildingName: '北区2栋', location: '一楼', lastMaintenanceDate: '2024-02-02', status: 20, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, name: '顶楼换气扇', buildingId: 3, buildingName: '北区3栋', location: '顶楼', lastMaintenanceDate: '2024-03-03', status: 10, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, name: '大厅电风扇', buildingId: 4, buildingName: '北区4栋', location: '大厅', lastMaintenanceDate: '2024-04-04', status: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (facilityForm: FacilityForm): Observable<Result<void>> {
    console.log('facilityApi save params', facilityForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (facilityQueryForm: FacilityQueryForm): Observable<Result<Page<Facility>>> {
    console.log('facilityApi find params', facilityQueryForm)
    return MockApi.queryPageSuccessfully(this.facilitys).pipe(
      map((response: Response<Result<Page<Facility>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Facility>> {
    console.log('facilityApi findById id', id)
    let target = {}
    for (const a of this.facilitys) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Facility>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('facilityApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
